package com.yby6.framework.web.exception;


import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.yby6.common.constant.HttpStatus;
import com.yby6.common.enums.ResultEnum;
import com.yby6.common.exception.BaseException;
import com.yby6.common.exception.CustomException;
import com.yby6.common.exception.DemoModeException;
import com.yby6.common.exception.YangBuYiException;
import com.yby6.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import com.yby6.common.core.AjaxResult;

import javax.security.auth.login.AccountExpiredException;
import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 *
 * @author yangbuyi
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
	private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	/**
	 * 基础异常
	 */
	@ExceptionHandler(BaseException.class)
	public AjaxResult baseException(BaseException e) {
		return AjaxResult.error(e.getMessage());
	}

	/**
	 * 业务异常
	 */
	@ExceptionHandler(CustomException.class)
	public AjaxResult businessException(CustomException e) {
		if (StringUtils.isNull(e.getCode())) {
			return AjaxResult.error(e.getMessage());
		}
		return AjaxResult.error(e.getCode(), e.getMessage());
	}

	/**
	 * 业务异常
	 */
	@ExceptionHandler(YangBuYiException.class)
	public AjaxResult yangBuYiException(YangBuYiException e) {
		log.error("统一异常处理：", e);
		return AjaxResult.error(e.getMessage());
	}

	/**
	 * 未发现处理程序异常
	 */
	@ExceptionHandler(NoHandlerFoundException.class)
	public AjaxResult handlerNoFoundException(Exception e) {
		log.error(e.getMessage(), e);
		return AjaxResult.error(HttpStatus.NOT_FOUND, "路径不存在，请检查路径是否正确");
	}


	/**
	 * 拦截未知的运行时异常
	 */
	@ExceptionHandler(RuntimeException.class)
	public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',发生未知异常.", requestURI, e);
		return AjaxResult.error(e.getMessage());
	}


	@ExceptionHandler(AccountExpiredException.class)
	public AjaxResult handleAccountExpiredException(AccountExpiredException e) {
		log.error(e.getMessage(), e);
		return AjaxResult.error(e.getMessage());
	}

	/**
	 * 全局异常
	 */
	@ExceptionHandler(Exception.class)
	public AjaxResult handleException(Exception e) {
		log.error(e.getMessage(), e);
		if (e != null){
			return AjaxResult.error("Exception Error");
		}else {
			return AjaxResult.error(ResultEnum.NOT_LOGIN.getMsg());
		}
	}

	/**
	 * 自定义验证异常
	 */
	@ExceptionHandler(BindException.class)
	public AjaxResult validatedBindException(BindException e) {
		log.error(e.getMessage(), e);
		String message = e.getAllErrors().get(0).getDefaultMessage();
		return AjaxResult.error(HttpStatus.BAD_REQUEST,message);
	}

	/**
	 * 自定义验证异常
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public Object validExceptionHandler(MethodArgumentNotValidException e) {
		log.error(e.getMessage(), e);
		String message = e.getBindingResult().getFieldError().getDefaultMessage();
		return AjaxResult.error(message);
	}

	/**
	 * 演示模式异常
	 */
	@ExceptionHandler(DemoModeException.class)
	public AjaxResult demoModeException(DemoModeException e) {
		log.error("demoModeException: {0}", e);
		return AjaxResult.error("演示模式，不允许操作");
	}

	/**
	 * 没登录异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(NotLoginException.class)
	public AjaxResult notLoginException(NotLoginException e){
		log.error("notLoginException: {0}", e);
		return AjaxResult.authErr("令牌失效，请重新登陆！");
	}

	/**
	 * 没权限异常
	 */
	@ExceptionHandler(NotPermissionException.class)
	public AjaxResult notPermissionException(NotPermissionException e){
		log.error("notPermissionException: {0}", e);
		return AjaxResult.error("不好意思您没有操作该权限的权力!");
	}


}
